﻿
using Microsoft.AspNetCore.Mvc;
using Spire.Doc;


namespace RsCms.WebApi.Controllers
{
	[Route("api/[controller]")]
	[ApiController]
	public class ConvertController : ControllerBase
	{
       
		/// <summary>
		/// word转html
		/// </summary>
		/// <returns></returns>
		[HttpPost("word2html")]
		public async Task<object> Doc2HtmlAsync()
		{
			var files = Request.Form.Files;

			if (files == null || files.Count == 0)
			{
				return BadRequest("没有指定doc文件");
			}
			var dir = Path.Combine(AppContext.BaseDirectory, "wwwroot", "public");
			if (!Directory.Exists(dir))
			{
				Directory.CreateDirectory(dir);
			}
			var file = files[0];

			// 指定文件保存的路径
			var fileName = $"{Guid.NewGuid().ToString("N")}.{file.FileName.Split(".")[1]}";
			var filePath = Path.Combine(dir, fileName);

			// 加载Word文档
			using (var stream=file.OpenReadStream())
			{
				Document doc = new Document(stream);
				doc.SaveToFile(filePath, FileFormat.Html);
			}

			return await System.IO.File.ReadAllTextAsync(filePath);
		}
	}
}
